package cn.iocoder.yudao.module.crm.controller.open.clue;

import cn.iocoder.yudao.framework.common.exception.ErrorCode;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.framework.operatelog.core.annotations.OperateLog;
import cn.iocoder.yudao.framework.tenant.core.util.TenantUtils;
import cn.iocoder.yudao.module.crm.controller.admin.clue.vo.CrmClueSaveReqVO;
import cn.iocoder.yudao.module.crm.controller.admin.cluepool.vo.CluePoolRespVO;
import cn.iocoder.yudao.module.crm.controller.open.clue.vo.CrmClueOpenSaveReqVO;
import cn.iocoder.yudao.module.crm.dal.dataobject.cluepool.CluePoolDO;
import cn.iocoder.yudao.module.crm.service.clue.CrmClueService;
import cn.iocoder.yudao.module.crm.service.cluepool.CluePoolService;
import cn.iocoder.yudao.module.system.api.tenant.TenantApi;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.List;
import java.util.Objects;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

@Tag(name = "OPEN - 线索")
@RestController
@RequestMapping("/crm/clue")
@Validated
public class OpenCrmClueController {

    @Resource
    private CrmClueService clueService;


    @Resource
    private CluePoolService cluePoolService;

    @Resource
    private TenantApi tenantApi;

    @PostMapping("/create")
    @Operation(summary = "创建线索")
    @OperateLog(enable = false)
    public CommonResult<Long> createClue(@Valid @RequestBody CrmClueOpenSaveReqVO createReqVO) {
        // 仍然需要传递tenantId + ownerUserid
        Long id = TenantUtils.execute(createReqVO.getTenantId(), () -> {
            Long ownerUserId = tenantApi.getContactUserIdByTenant(createReqVO.getTenantId());
            if (Objects.isNull(ownerUserId)) {
                throw exception(new ErrorCode(100500, "未知用户"));
            }
            CrmClueSaveReqVO saveReqVO = BeanUtils.toBean(createReqVO, CrmClueSaveReqVO.class);
            saveReqVO.setOwnerUserId(ownerUserId);
            return clueService.createClueWithoutUser(saveReqVO);
        });
        // 判断source 来源：12 插入机器人聊天记录； 其他的再说

        return success(id);
    }

    @GetMapping("/get-pool-list")
    @Operation(summary = "线索公海列表")
    @OperateLog(enable = false)
    @Deprecated
    public CommonResult<List<CluePoolRespVO>> getCluePoolList(@RequestParam("tenantId") Long tenantId) {
        List<CluePoolDO> list = cluePoolService.getEnableClueListByTenant(tenantId);
        return success(BeanUtils.toBean(list, CluePoolRespVO.class));
    }


//    @GetMapping("/page")
//    @Operation(summary = "获得线索分页")
//    @OperateLog(enable = false)
//    public CommonResult<PageResult<CrmClueRespVO>> getCluePage(@Valid CrmCluePageReqVO pageVO) {
//        PageResult<CrmClueDO> pageResult = clueService.getCluePage(pageVO, getLoginUserId());
//        return success(BeanUtils.toBean(pageResult, CrmClueRespVO.class));
//    }
}
